/*
 * solver.c
 *
 *  Created on: 30/12/2011
 *      Author: marcio
 */

#include "solver.h"
#include "formulation.h"

extern long k;
extern CPXENVptr env;//ambiente do cplex
extern CPXLPptr prob;//problema inteiro
extern long* va;

/*cria a formulação*/
int createFormulation(){
	setAdjMatrix(getSharedGraph());
	int i, j, m, ckeck;
	init();
	for(i = 0; i < nverts(); i++){
		addCoverConstrain(i);
		for(j = i+1; j < nverts(); j++){
			if(hasEdge(va[i], va[j])){
				ckeck = 0;
				for(m = i+1; m < nverts(); m++){
					if(!hasEdge(va[j], va[m]) && hasEdge(va[i], va[m]) && m!= j){
						ckeck = 1;
						if(m > j)addEdgeConstrain(va[i],va[j],va[m]);
					}
				}
				if(ckeck == 0){
					addIndConstrain(va[i],va[j]);
				}
			}
		}
	}
	return 0;
}

/*impremi a formualação*/
int printFormulation(){
	return CPXwriteprob (env, prob, "problemalinear.lp", NULL);
}

/*resolve a formulação*/
double solve(){
	CPXsetstrparam(env, CPX_PARAM_TILIM, 360);
	CPXlpopt(env, prob);
	double value;
	CPXgetobjval(env,prob, &value);
	/*double x[nverts()*nverts()*k];
	CPXgetx(env, prob, x, 0, CPXgetnumcols(env, prob)-1);
	int i ;
	for(i = 0; i < CPXgetnumcols(env, prob)-1; i++){
		printf("variavel %d = %f \n", i+1, x[i]);
	}*/
	return value;
}

int deleteSolver(){
	deleteFormulation();
	return 0;
}
